import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-12-12
 * Time: 18:48
 */
class Solution {
    public int calculate(String ss) {
        // 1：new 一个栈
        Stack<Integer> stack = new Stack<>();
        // 2：遍历字符串
        char[] s = ss.toCharArray();
        int i = 0 , n = s.length;
        int op = '+';
        while(i < n){
            // 3：分情况讨论，重点是确定tem和字符的情况
            if(s[i] == ' ') i++;
                // 4：利用字符的ASCII码值进行字符和整型之间的转换
            else if(s[i] >= '0' && s[i] <= '9'){
                int tem = 0;
                while( (i < n) && (s[i] >= '0' && s[i] <= '9') ){
                    tem = 10 * tem + (s[i] - '0');
                    i++;//这里的if判断条件太巧妙了利用ASCII值,并且i++后的处理真是绝了
                }
                if(op == '+') stack.push(tem);
                else if(op == '-') stack.push(-tem);
                else if(op == '*') stack.push(stack.pop() * tem);
                else if(op == '/') stack.push(stack.pop() / tem);
            }else{
                op = s[i];
                i++;
            }
        }

        int ret = 0;
        while(!stack.isEmpty()){
            ret += stack.pop();
        }
        return ret;


    }

}
public class Test1_3 {
}
